/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Address;

import Country.CountryBean;
import State.StateBean;
import java.sql.*;
import java.util.Vector;

/**
 *
 * @author mishra
 */
public class Query {

    static Connection con = null;

    public static int add(AddressBean ab) throws SQLException {
        int i = 0, k;

        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("insert into address values(addressid.nextval,?,?,?,?,?,sysdate)");
            pre.setString(1, ab.getAddress1());
            pre.setString(2, ab.getAddress2());
            pre.setInt(3, ab.getCountryId());
            pre.setInt(4, ab.getStateId());
            pre.setString(5, ab.getPinCode());
            k = pre.executeUpdate();
            if (k != 0) {
                i = getMaxId();
            }

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }

    public static int getMaxId() throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select max(address_id) from address");
            ResultSet rs = pre.executeQuery();
            if (rs.next()) {
                i = rs.getInt(1);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return i;
    }

    public static AddressBean viewSpecific(String cname, int val) throws SQLException {

        AddressBean ab = new AddressBean();

        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from address where " + cname + "=?");
            pre.setInt(1, val);
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                CountryBean cb = new CountryBean();
                StateBean sb = new StateBean();
                ab.setAddressId(rs.getInt(1));
                ab.setAddress1(rs.getString(2));
                ab.setAddress2(rs.getString(3));
                ab.setCountryId(rs.getInt(4));
                ab.setStateId(rs.getInt(5));
                ab.setPinCode(rs.getString(6));
                ab.setEntryDate(rs.getDate(7));
                cb.setCountryId(rs.getInt(8));
                cb.setCountryName(rs.getString(9));
                cb.setEntryDate(rs.getDate(10));
                ab.setCountryBean(cb);
                sb.setStateId(rs.getInt(11));
                sb.setStateName(rs.getString(12));
                sb.setEntryDate(rs.getDate(13));
                ab.setStateBean(sb);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return ab;

    }

    public static Vector viewAll() throws SQLException {
        Vector<Object> list = new Vector<Object>();

        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from address order by address_id");
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                AddressBean ab = new AddressBean();
                CountryBean cb = new CountryBean();
                StateBean sb = new StateBean();
                ab.setAddressId(rs.getInt(1));
                ab.setAddress1(rs.getString(2));
                ab.setAddress2(rs.getString(3));
                ab.setCountryId(rs.getInt(4));
                ab.setStateId(rs.getInt(5));
                ab.setPinCode(rs.getString(6));
                ab.setEntryDate(rs.getDate(7));
                cb.setCountryId(rs.getInt(8));
                cb.setCountryName(rs.getString(9));
                cb.setEntryDate(rs.getDate(10));
                ab.setCountryBean(cb);
                sb.setStateId(rs.getInt(11));
                sb.setStateName(rs.getString(12));
                sb.setEntryDate(rs.getDate(13));
                ab.setStateBean(sb);
                list.add(ab);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return list;

    }

    public static int edit(AddressBean ab) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("update address set address1=?, address2=?, country_id=?,state_id=?, pincode=?, entrydate=sysdate where address_id=?");
            pre.setString(1, ab.getAddress1());
            pre.setString(2, ab.getAddress2());
            pre.setInt(3, ab.getCountryId());
            pre.setInt(4, ab.getStateId());
            pre.setString(5, ab.getPinCode());
            pre.setInt(6, ab.getAddressId());
            i = pre.executeUpdate();

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }

    public static int delete(int id) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("delete from address where address_id=?");
            pre.setInt(1, id);
            i = pre.executeUpdate();
        } catch (Exception e) {
            return i;
        } finally {
            con.close();
        }
        return i;
    }
    
 
}
